package com.kanqiubao.kq.activity.live;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.angcyo.tablayout.DslTabLayout;
import com.angcyo.tablayout.delegate.ViewPager1Delegate;
import com.bumptech.glide.Glide;
import com.dueeeke.videocontroller.StandardVideoController;
import com.dueeeke.videocontroller.component.CompleteView;
import com.dueeeke.videocontroller.component.ErrorView;
import com.dueeeke.videocontroller.component.GestureView;
import com.dueeeke.videocontroller.component.LiveControlView;
import com.dueeeke.videocontroller.component.PrepareView;
import com.dueeeke.videocontroller.component.TitleView;
import com.dueeeke.videoplayer.player.VideoView;
import com.kanqiubao.kq.R;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.fragment.BaseFragment;
import com.kanqiubao.kq.fragment.NewsFragmentStatePagerAdapter;
import com.kanqiubao.kq.fragment.live.LiveDetailFragmOne;
import com.kanqiubao.kq.fragment.live.LiveDetailFragmentFive;
import com.kanqiubao.kq.fragment.live.LiveDetailFragmentFour;
import com.kanqiubao.kq.fragment.live.LiveDetailFragmentThree;
import com.kanqiubao.kq.fragment.live.LiveDetailFragmentTwo;
import com.kanqiubao.kq.view.LiveAttachListPopupView;
import com.kanqiubao.kq.view.player.LiveTitleView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;

import java.util.ArrayList;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * 直播详细
 */
public class LiveDetailActivity extends BaseActivity<String> {
    private DslTabLayout mDslTabLayout;
    private ViewPager mViewPager;
    private NewsFragmentStatePagerAdapter mNewsFragmentStatePagerAdapter;
    private ArrayList<Fragment> mListFragments;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mDslTabLayout = findView(R.id.live_tab_id);
        mViewPager = findView(R.id.live_page_id);
        mVideoView = findView(R.id.video_id);

        initFragment();

        initPlayer();
    }

    private VideoView mVideoView;

    /**
     * 初始化播放器
     */
    private void initPlayer() {
        StandardVideoController controller = new StandardVideoController(this);
        //根据屏幕方向自动进入/退出全屏
        controller.setEnableOrientation(true);
        PrepareView prepareView = new PrepareView(this);//准备播放界面
        ImageView thumb = prepareView.findViewById(R.id.thumb);//封面图
//        Glide.with(this).load(THUMB).into(thumb);
        controller.addControlComponent(prepareView);

        controller.addControlComponent(new CompleteView(this));//自动完成播放界面

        controller.addControlComponent(new ErrorView(this));//错误界面


        LiveTitleView titleView = new LiveTitleView(this);//标题栏
        controller.addControlComponent(titleView);
        controller.addControlComponent(new LiveControlView(this));//直播控制条
        GestureView gestureControlView = new GestureView(this);//滑动控制视图
        controller.addControlComponent(gestureControlView);
        //根据是否为直播决定是否需要滑动调节进度
        controller.setCanChangePosition(false);

        //如果你不想要UI，不要设置控制器即可
        mVideoView.setVideoController(controller);
        mVideoView.setUrl("http://220.161.87.62:8800/hls/0/index.m3u8");
        mVideoView.start();

    }

    @Override
    protected int getActivitylayout() {
        return R.layout.live_detail_layout;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.change_user_id:
                changeZb(v);
                break;
        }
        super.click(v);
    }

    /**
     * 切换主播dialog
     */
    private void changeZb(View v) {
        new XPopup.Builder(this)
                .hasShadowBg(false)
                .atView(v)  // 依附于所点击的View，内部会自动判断在上方或者下方显示
                .asCustom(new LiveAttachListPopupView(this, 0, R.layout.live_detail_popu_item_layout)
                        .setStringData(new String[]{"主播1", "主播222"}, new String[]{
                                "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3886283169,602937879&fm=26&gp=0.jpg"
                                , "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3886283169,602937879&fm=26&gp=0.jpg"
                        }).setOnSelectListener(new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                            }
                        })).show();
    }

    private void initFragment() {
        mNewsFragmentStatePagerAdapter = new NewsFragmentStatePagerAdapter(getSupportFragmentManager());
        mListFragments = new ArrayList<>();
        LiveDetailFragmOne item1 = new LiveDetailFragmOne();
        item1.setmTitle("聊天");
        LiveDetailFragmentTwo item2 = new LiveDetailFragmentTwo();
        item2.setmTitle("主播");
        LiveDetailFragmentThree item3 = new LiveDetailFragmentThree();
        item3.setmTitle("直播");
        LiveDetailFragmentFour item4 = new LiveDetailFragmentFour();
        item4.setmTitle("数据");
        LiveDetailFragmentFive item5 = new LiveDetailFragmentFive();
        item5.setmTitle("榜单");
        mListFragments.add(item1);
        mListFragments.add(item2);
        mListFragments.add(item3);
        mListFragments.add(item4);
        mListFragments.add(item5);
        for (int i = 0; i < mListFragments.size(); i++) {
            View v = LayoutInflater.from(this).inflate(R.layout.layout_txt_view, null);
            ((TextView) v.findViewById(R.id.text_view)).setText(((BaseFragment) mListFragments.get(i)).getmTitle());
            mDslTabLayout.addView(v);
        }
        mNewsFragmentStatePagerAdapter.setData(mListFragments);
        mViewPager.setAdapter(mNewsFragmentStatePagerAdapter);
        ViewPager1Delegate viewPager1Delegate = new ViewPager1Delegate(mViewPager, mDslTabLayout);
        mDslTabLayout.setupViewPager(viewPager1Delegate);
    }
}
